<?php
class SetuserinfoController extends AppController
{
	
	var $autoRender = true;
    var $uses = array( 'Users','User_expand' );
    var $layout = 'default';
    private $helpers;
    function init()
    {
    	$this->helpers = array( 'html' );
    }
    function set_base_info() //修改资料
    {	
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$user_id = $_SESSION['user_id'];
    	$nick_name = filter_content( init_web_para( 'nick_name', '' ) );
    	$sex = init_web_para( 'sex', 0 );
    	$pro = init_web_para( 'pro', 0 );
    	$edu = init_web_para( 'edu', 0 );
    	$tag = filter_content( init_web_para( 'tag', '' ) );
    	$brief = filter_content( init_web_para( 'brief', '' ) );
    	$ret_user = $ret_info = false;
    	$msg = '';
    	$ret = 0;
    	if( $nick_name != '' )
    	{
    		$ret_temp = $this->Users->findAll( array( 'nick_name' => $nick_name ), null, null, 1 );
    		if( count( $ret_temp ) > 0 && $ret_temp[0]['Users']['id'] != $user_id )
    		{
    			$ret = 1;
    			$msg .= '昵称不允许重复';
    		}
    		else 
    		{
    			$update_user = array( 
    				'id' => $user_id,
    				'nick_name' => $nick_name, 
    				'update_time' => $curr_time
    			);
    			$ret_user = $this->Users->save( $update_user );
    		}
    	}
    	$ret_info_temp = $this->User_expand->findAll( array( 'user_id' => $user_id ), null, null, 1 );
    	if( count( $ret_info_temp ) > 0 )
    	{
    		$update_info = array(
    			'id' => $ret_info_temp[0]['User_expand']['id'],
    			'sex' => $sex,	
		    	'pro' => $pro,
		    	'edu' => $edu,
		    	'tag' => $tag,
		    	'brief' => $brief,
    			'update_time' => $curr_time
    		);
    	}
    	else 
    	{
    		$update_info = array(
    			'id' => null,
    			'user_id' => $user_id,
    			'sex' => $sex,	
		    	'pro' => $pro,
		    	'edu' => $edu,
		    	'tag' => $tag,
		    	'brief' => $brief,
    			'create_time' => $curr_time,
    			'update_time' => $curr_time
    		);
    	}
    	$ret_info = $this->User_expand->save( update_info );
    	if( $ret_user !== true )
    	{
    		$ret = 1;
    		$msg .= '修改昵称失败';
    	}
    	if( $ret_info !== true )
    	{
    		$ret = 1;
    		$msg .= '修改用户信息失败';
    	}
    }
    function modify_pwd( $pwd )//修改密码
    {
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$pwd = filter_content( $pwd );
    	$user_id = $_SESSION['user_id'];
    	$ret = false;
    	$msg = '';
    	if( strlen( $pwd ) == MDLENTH )
    	{
    		$update_pwd = array(
    			'id' => $user_id,
    			'pwd' => $pwd,
    			'update_time' => $curr_time
    		);
    		$ret = $this->Users->save( $update_pwd);
    	}
    	else
    	{
    		$msg = '密码被篡改';
    	}
    }
    function upload_head_img()
    {
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$file_list = array();
    	$user_id = $_SESSION['user_id'];
    	$msg = '';
    	$ret = 0;
    	$ret_uplode = upload_image( $user_id,UPLOAD_HEAD_IMG_PATH,$file_list,$msg );
    	if( $ret_uplode === true )
    	{
    		$update_user = array(
    				'id' => $user_id,
    				'head_img' => $file_list[0],
    				'update_time' => $curr_time
    		);
    		$ret_user = $this->Users->save( $update_user );
    		if( $ret_user === true )
    		{
    			$ret = 1;
    		}
    		else
    		{
    			$msg = '上传头像失败';
    		}
    	}
    }
}